import { getRoleConfig } from "utils/constants/bodyconstant";
import { BodyUtils } from "./bodyutils";
export const CreepActionEffect:Record<string,number>=
{
    build:0.5,
    repair:0.6,
    upgradeController:0.9
}
export const CreepWorkProgress:Record<string,number>=
{
    build:5,
    repair:100,
    upgradeController:1
}

export const  CreepWorkEnergyConsume:Record<string,number>=
{
    build:5,
    repair:1,
    upgradeController:1
}
/**
 * body的工作效率计算
 */
export class BodyEfficiency
{

    /**
     * 获取work工时
     * @param creeps 
     * @returns 
     */
    static getCreepWorkTick(creeps:Creep[])
    {
        return _.sum(creeps,e=>
            {
                if(e&& e.ticksToLive)
                {
                    return e.getActiveBodyparts(WORK)* e.ticksToLive
                }
                return 0;
            })
    }

    /**
     * 获取这个pa在死之前能够满负荷工作的 工作进度
     * @param creeps 
     * @param action 
     * @returns 
     */
    static getWorkProgress(creeps:Creep[],action:"build"|"repair"|"upgradeController")
    {
        const worktick = this.getCreepWorkTick(creeps);
        return worktick* CreepWorkProgress[action];
    }

    /**
     * 计算在当前资源限制下。最多可孵化的角色数量
     * @param left 剩余可使用资源
     * @param level 房间等级
     * @param role 角色
     * @param action 行为
     * @returns 
     */
    public static getMaxCreepAmount(left:{energy:number,body:number},level:number,role:RoleName,action:"build"|"repair"|"upgradeController")
    {
        const bodyconfig =   getRoleConfig(role,level);
        const bodyparts =  BodyUtils.flatBodyParts(bodyconfig.body); // 
        const rolecost = BodyUtils.calculateSpawnEnergy(bodyparts);     // 孵化消耗的能量

        const rolecount_bybody = Math.floor(left.body/(bodyparts.length +1.0/3));
     

        const ttl = 1500*0.9;// 近似有9成时间在干活
        const workcost = bodyparts.filter(e=>e== WORK).length * CreepWorkEnergyConsume[action]*ttl

        const rolecount_byenergy = Math.floor(left.energy/(rolecost+workcost));
        return Math.min(rolecount_bybody,rolecount_byenergy)
    }

    /**
     * 根据角色和等级 和有效body类型 和body的个数。计算角色的数量
     * @param role 
     * @param level 
     * @param body 
     * @param number 
     * @returns 
     */
    public static getCreepCount(role:RoleName,level:number,body:BodyPartConstant,number:number)
    {
        const bodyconfig =  getRoleConfig(role,level);
        const work_amont = BodyUtils.getCreepCount(bodyconfig.body,body,number);
        return work_amont
    }

    /**
     * 计算这个进度。需要多少个role 角色的爬才能干完
     * @param progress 
     * @param role 
     * @param level 
     * @param action 
     * @returns 
     */
    public static calCreepCountByProgress(progress:number,body: [BodyPartConstant, number][],action:"build"|"repair"|"upgradeController")
    {
        // const bodyparts =  BodyUtils.flatBodyParts(bodyconfig.body); // 
        const worknum = progress/(CreepWorkProgress[action]*1500);

        const work_amont = BodyUtils.getCreepCount(body,WORK,worknum);

        return work_amont;
    }
}